package com.travelsupport.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.Toast;

import com.travelsupport.common.Common;
//import com.travelsupport.common.MyFavoriteListViewAdapter;
import com.travelsupport.myobject.LocationObj;

public class MyFavoriteActivity extends Activity {
	ListView lvFavorite;
	List<LocationObj> favoriteItems;
	//MyFavoriteListViewAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_favorite);

		lvFavorite = (ListView) findViewById(R.id.lvFavorite);
		favoriteItems = new ArrayList<LocationObj>();

		favoriteItems.add(new LocationObj() {
			{
				setImage(Common
						.convertToByte(getImageFromDrawable(R.drawable.default_icon)));
				setName("Fake data 1");
				setChecked(false);
			}
		});
		favoriteItems.add(new LocationObj() {
			{
				setImage(Common
						.convertToByte(getImageFromDrawable(R.drawable.default_icon)));
				setName("Fake data 2");
				setChecked(false);
			}
		});

		//adapter = new MyFavoriteListViewAdapter(this, favoriteItems);
		//lvFavorite.setAdapter(adapter);
		lvFavorite.setEmptyView(findViewById(R.id.txtNothing));

		CheckBox cbAll = (CheckBox) findViewById(R.id.cbAll);
		cbAll.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean checked) {
				// TODO Auto-generated method stub
				if (checked == true) {
					for (LocationObj item : favoriteItems) {
						item.setChecked(true);
					}
				//	lvFavorite.setAdapter(adapter);
				}
				if (checked == false) {
					for (LocationObj item : favoriteItems) {
						item.setChecked(false);
					}
			//		lvFavorite.setAdapter(adapter);
				}
			}
		});
	}

	// get image from drawable
	public Bitmap getImageFromDrawable(int imageToConvert) {
		Bitmap image = BitmapFactory.decodeResource(getResources(),
				imageToConvert);
		return image;
	}

	public void Remove(View view) {
		// for (LocationObj item : favoriteItems) {
		// if (item.isChecked() == true) {
		// favoriteItems.remove(item);
		// }
		// }

		// for (int i = 0; i < favoriteItems.size(); i++) {
		// LocationObj item = new LocationObj();
		// if (adapter.getItem(i).isChecked() == true) {
		// favoriteItems.remove(i);
		// }
		// }
		// adapter = new MyFavoriteListViewAdapter(this, favoriteItems);
		// lvFavorite.setAdapter(adapter);
		Toast.makeText(this, "Remove selected item(s)", Toast.LENGTH_SHORT)
				.show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_my_favorite, menu);
		return true;
	}

}